package team.yidong.fenda.wx.Service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.yidong.fenda.entity.Answer;
import team.yidong.fenda.entity.Master;
import team.yidong.fenda.entity.MasterScore;
import team.yidong.fenda.entity.Question;
import team.yidong.fenda.repository.AnswerRepository;
import team.yidong.fenda.repository.MasterRepository;
import team.yidong.fenda.repository.MasterScoreRepository;
import team.yidong.fenda.repository.QuestionRepository;
import team.yidong.fenda.wx.Service.WxAcceptService;

import java.util.Set;

/**
 * Created by xuanlong on 2017/11/8.
 */
@Service
public class WxAcceptServiceImpl implements WxAcceptService {
    @Autowired
    AnswerRepository answerRepository;
    @Autowired
    MasterRepository masterRepository;
    @Autowired
    MasterScoreRepository masterScoreRepository;
    @Autowired
    QuestionRepository questionRepository;

    @Transactional
    @Override
    public String accept(Integer answerId, Double money, String getDate) {
        Answer answer = answerRepository.findOne(answerId);
        Question question = answer.getQuestion();
        Master master = answer.getMaster();
        //采纳时增加专家的积分
        MasterScore masterScore =answer.getMasterScore();
        masterScore.setNum(money);

        masterScore.setCreatDate(getDate);

        masterScoreRepository.save(masterScore);
        //减少用户的问题积分
        question.setRestMoney(question.getRestMoney() - money);
        if (question.getRestMoney() < 0) {
            return "输入的金额过大";
        } else {
            questionRepository.save(question);
            return "采纳成功";
        }


    }
}
